package bg.btanev.eightyfour.domain.experience;

import java.util.Date;

import org.apache.commons.lang.Validate;

import bg.btanev.eightyfour.domain.entitybase.DomainValue;

/**
 *
 * @author B.Tanev
 *
 */
public class ExperiencePeriod implements DomainValue, Comparable<ExperiencePeriod> {
	/**
	 *
	 */
	private static final long serialVersionUID = -8665493059901541467L;

	private Date begin;
	private Date end;

	protected ExperiencePeriod() {
		super();
	}

	private ExperiencePeriod(Date begin, Date end) {
		super();

		Validate.notNull(begin, "Experience begin date cannot be null.");
		if (end != null) {
			if (end.getTime() <= begin.getTime()) {
				throw new IllegalArgumentException("End date cannot be before begin date.");
			}
		}

		this.begin = begin;
		this.end = end;
	}

	public static ExperiencePeriod openPeriod(Date beginDate) {
		return new ExperiencePeriod(beginDate, null);
	}

	public static ExperiencePeriod closedPeriod(Date beginDate, Date endDate) {
		return new ExperiencePeriod(beginDate, endDate);
	}

	public boolean isOpenEnded() {
		return this.end != null;
	}

	public Date getBeginDate() {
		return begin;
	}

	public Date getEndDate() {
		return end;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((end == null) ? 0 : end.hashCode());
		result = prime * result + ((begin == null) ? 0 : begin.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ExperiencePeriod other = (ExperiencePeriod) obj;
		if (end == null) {
			if (other.end != null)
				return false;
		} else if (!end.equals(other.end))
			return false;
		if (begin == null) {
			if (other.begin != null)
				return false;
		} else if (!begin.equals(other.begin))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "ExperiencePeriod [endDate=" + end + ", beginDate=" + begin + "]";
	}

	@Override
	public int compareTo(ExperiencePeriod o) {
		return this.begin.compareTo(o.begin);
	}
}
